package lib

import (
	"flag"
	"os"

	"github.com/spf13/viper"
)

var (
	C    *Config
	file string
)

// InitConf 初始化项目配置
func InitConf() (*Config, error) {
	path, err := os.Getwd()
	if err != nil {
		return nil, err
	}
	flag.StringVar(&file, "f", path+"/conf/config.yaml", "config file path")
	flag.Parse()
	viper.SetConfigFile(file)
	if err = viper.ReadInConfig(); err != nil {
		return nil, err
	}
	if err = viper.Unmarshal(&C); err != nil {
		return nil, err
	}
	return C, nil
}

type Config struct {
	Logger *Logger
	Mysql  *Mysql
	GRPC   *GRPC
	Redis  *Redis
	Queue  *Queue
	File   *File
}

type GRPC struct {
	Port int32 // grpc端口
}

type Logger struct {
	Level    string   // 错误等级
	Debug    bool     // 是否打印错误
	FilePath string   // 文件路径
	TraceKey []string // 链路追踪keys
}

type Mysql struct {
	URL     string // 数据库连接
	MaxIdle int    // 最大连接数
	FPath   string // 文件路径
	ShowSQL bool   // 是否打印sql
}

type Redis struct {
	Dsn      string // URL
	Password string // 密码
	MaxIdle  int    // 最大空闲数
	DB       int    // DB
}

type File struct {
	Driver string
	URL    string
}

type Queue struct {
	Kafka    *kafkaMQ
	RocketMQ *rocketMQ
	RedisMQ  *redisMQ
}
type rocketMQ struct {
	Driver    string
	State     bool
	EndPoints string
	Retry     int
}
type kafkaMQ struct {
	Driver     string
	EndPoints  string
	Version    string
	State      bool
	RandClient bool
	Retry      int
}
type redisMQ struct {
	Driver    string
	State     bool
	EndPoints string
	Retry     int
	PWD       string
	DBNum     int
	Timeout   int
}
